#ifndef DATA_STRUCT_H
#define DATA_STRUCT_H
#include "stdint.h"
#include "fpga_config.h"

#define R_VERSION   DATA_VERSION


#define DATA_MESSAGE_D2H    0x41    //device to Host
#define DATA_MESSAGE_H2D    0x00    //host to device
#define RES_MESSAGE_HEAD    0x04


#define  MAX_MSG_BUFFER     512
#define MSG_HEAD_CONST  0x55AA0051
enum  VPD_MSG_TYPE
{
    MSG_START,
    MSG_STOP,
    MSG_PAUSE,
    MSG_SET_SAMPLE_RATE,
    MSG_SET_MIC_INDEX,
    MSG_SET_FREQ_RANGE,
    MSG_SET_DISTANCE,
    MSG_SET_LIGHT,
    MSG_SET_BEAM_INFO,
    MSG_SET_PIC_INFO,
    MSG_SET_TCP_DATA,               //C
    MSG_HEART_RATE,
    MSG_GET_BEAM_INFO,
    MSG_SET_BEAM_RESOLUTION,        //resolution
    MSG_UPDATE_FPGA,                //Update FPGA
    MSG_UPDATE_RKDEV,               //UPDate RK CPU
    MSG_SET_FILTER_COE,             //set coe filter  factor
    MSG_GET_FILTER_COE,             //get coe filter factor
    MSG_GET_MIC_COORD,              //get mic coords
    MSG_SET_MIC_COORD,              //get mic coords
    MSG_SET_MIC_FILTER,
    MSG_SET_MIC_CHAN_SIZE,          //set mic channels size
    MSG_SET_MIC_CHAN_REMAP,         //set mic channels 2 hardware
	MSG_SET_MIC_DELAY,
	MSG_SET_MIC_MASK,				//set mic Mask
    MSG_SET_RAWFFT_FPS=0x51,
    MSG_SET_MASTER=0x52,
    MSG_SET_TRIGGER_MODE,
    MSG_SET_TRIGGER,
    MSG_SYSTEM_CMD=0xFE,                 //run system cmd
};
enum DEVICE_STATUS
{
    STATUS_IDLE=0,
    STATUS_MEASURING,
    STATUS_STREAMING,
    STATUS_SERVICE,
    STATUS_ULTRA,
    STATUS_ULTRA_P1,
    STATUS_UPDATE,
};
enum DEVICE_SUB_STATUS
{
    SUB_IDLE=0,
    SUB_RUNNING=1,
    SUB_PREPARING=116,
    SUB_FINISHED=128,
    SUB_ABORTED=129,
    SUB_LOCKED=204,
	SUB_FFT_SHORT=512,
	SUB_FFT_LONG=1024,
};

enum LIST_DATA_TYPE
{
    TYPE_RAW_DATA,
    TYPE_FFT_DATA_V1,
    TYPE_FFT_DATA_V2,
};
enum REQUEST_CMD
{
    REQ_CMD_RESET  = 0x80,
    REQ_CMD_IDENTIFICATION =0x84,
    REQ_CMD_PREPARE_STATE  = 0x86,
    REQ_CMD_FINISH_STATE   = 0x87,
    REQ_CMD_STOP_PROCEDURE  =0x89,
    REQ_CMD_START_PROCEDURE =0x8A,
    REQ_CMD_READ_DATA_OBJECT    =0x92,
    REQ_CMD_WRITE_DATA_OBJECT   =0x93,
};
enum RESPONSE_CMD
{
    RES_CMD_RESET  = 0x00,
    RES_CMD_IDENTIFICATION =0x04,
    RES_CMD_PREPARE_STATE  = 0x06,
    RES_CMD_FINISH_STATE    =0x07,
    RES_CMD_STOP_PROCEDURE  =0x09,
    RES_CMD_START_PROCEDURE =0x0A,
    RES_CMD_READ_DATA_OBJECT    =0x12,
    RES_CMD_WRITE_DATA_OBJECT   =0x13,
};


enum OBJ_ID{
    OBJ_ID_COMMON_STATUS        =0x0001,
    OBJ_ID_DISTANCE             =0x0002,
    OBJ_ID_FREQUENCY_RANGE      =0x0003,
    OBJ_ID_CAMERA_RESOLUTION    =0x0004,
    OBJ_ID_CAMERA_BRIGHTNESS    =0x0005,
    OBJ_ID_ACOUSTIK_FRAME_RATE  =0x0006,
    OBJ_ID_VIDEO_FRAME_RATE     =0x0007,
    OBJ_ID_CAMERA_LIGHTING      =0x0008,
    OBJ_ID_MICROPHONE_POSITION  =0x0009,
    OBJ_ID_MICROPHONE_WEIGHTING =0x000B,
    OBJ_ID_ALIGNMENT            =0x000C,
    OBJ_ID_VIDEO_DATA           =0x000D,
    OBJ_ID_ACOUSTIC_VIDEO_DATA  =0x000E,
    OBJ_ID_SPECTRUMDATA         =0x000F,
    OBJ_ID_CAMERA_STATUS        =0x0011,
    OBJ_ID_SELFTEST_STATUS      =0x0012,
    OBJ_ID_LS_TARGE_COOR        =0x0013,
    OBJ_ID_MICROPHONE_INDEX     =0x0014,
    OBJ_ID_RAW_DATA             =0x0019,
    OBJ_ID_PRODUCTION_DATA      =0x001A,
    OBJ_ID_MICROPHONE_STATUS    =0x001E,
    OBJ_ID_DATA_TO_SEND         =0x0020,
    OBJ_ID_ACTIVE_ARRAY         =0x0021,
    OBJ_ID_ARRAYS_AVAILABLE     =0x0022,
    OBJ_ID_ARRAY_SERIAL_NUMBER  =0x0023,
    OJB_ID_ACOUST_AVERAGING     =0x0106,
    OBJ_ID_CURRENT_DATA_TIME    =0xC000,
    OJB_ID_MIC_LEVELS           =0xC001,
    OBJ_ID_RAW_DATA_STREAM      =0xC002,
    OBJ_ID_FFT_DATA             =0xD000,
    OBJ_ID_RAW_FFT              =0xD001,
    OBJ_ID_REPLY_MESSAGE        =0xD002,    //  reply message
};
enum DATA_TYPE
{
    DT_BOOL=0x0001,
    DT_INT8,
    DT_UINT8,
    DT_INT16,
    DT_UINT16,
    DT_INT32,
    DT_UINT32,
    DT_INT64,
    DT_UINT64,
    DT_FLOAT,
    DT_DOUBLE,
    DT_STRING,
    DT_DATE,
    DT_DATE_TIME,
    DT_COMMON_STATUS,
    DT_MAC_ADDRERSS,
    DT_ARRAY_BOOL,
    DT_ARRAY_INT8,
    DT_ARRAY_UINT8,
    DT_ARRAY_INT16,
    DT_ARRAY_UINT16,
    DT_ARRAY_INT32,
    DT_ARRAY_UINT32,
    DT_ARRAY_INT64,
    DT_ARRAY_UINT64,
    DT_ARRAY_FLOAT,
    DT_ARRAY_DOUBLE,
    DT_FXP24,
    DT_FREQUENCY_RANGE=0x0100,
    DT_RESOLUTION=0x0200,
    DT_LOCAL_SOUND_TARGET=0x0201,
    DT_POSITION=0x0300,
    DT_MICROPHONE_WEIGHTING=0x0400,
    DT_MICROPHONE_ALIGNMENT=0x500,
    DT_VIDEO_DATA=0x0600,
    DT_ACOUSTIC_VIDEO_DATA=0x0700,
    DT_SPECTRUM_DATA=0x0800,
    DT_RAW_DATA=0x0801,
    DT_AUDIO_DATA=0x0900,
    DT_MICROPHONE_INDEX=0x0A00,
    DT_PRODUCTION_DATA=0x0C00,  //36
    DT_CAMERA_STATUS=0xC02,
    DT_MIC_LEVELS=0xC001
};
#pragma pack(1)


typedef struct
{
    uint32_t  MSG_HEAD;     // 0x55AA0051
    uint32_t  MSG_ID;
    uint32_t  MSG_TYPE;
    uint32_t  MSG_LENGTH;
    char      MSG_ARRAY[12*4];
}VPD_MESSAGE;
typedef struct BEAM_INFO
{
    float    FPS;
    float   xMin;
    float   xMax;
    float   dx;
    float   yMin;
    float   yMax;
    float   dy;
    float   dist;
    float   xyAngle;
    float   kFactor;
}beamInfo;
typedef struct TCP_SEND_MASK
{
    uint8_t      mRawdataEn;
    uint8_t      mOnedataEn;
    uint8_t      mRawFFTEn;
    uint8_t      mOneFFTEn;
    uint8_t      mPicEn;
    uint8_t      mAcousEn;
}tcpSendMask;
typedef struct VPD_CMD
{
    uint8_t cmd;
    uint8_t invokeId;
    uint8_t	cmdType;
    uint8_t	buffer[MAX_MSG_BUFFER];
}vpdCmd;
struct DATE_STRUCT
{
    uint16_t year;
    uint8_t month;
    uint8_t day;
};
struct OBJECT_HEAD
{
    uint16_t objID;
    uint16_t objType;
    uint32_t length;
};
typedef struct REQ_IDENTIFICATION_STRUCT
{
    uint8_t cmd;
    uint8_t invokeId;
    uint16_t reserved;
    uint32_t  dataLength;
    uint32_t  protocolVersion;
}reqIdentification;

typedef struct RES_IDENTIFICATIONS_STRUCT
{
    uint8_t cmd;    //=RES_IDENTIFICATION;
    uint8_t invokeId;
    uint16_t reserved;
    uint32_t status;    //=0;
    uint32_t dataLength;    //=76;
    uint32_t dataProtcolVersion;//=R_VERSION 2;
    uint32_t deviceId;
    uint32_t deviceMode;    //=1;
    uint32_t deviceError;       //=0; //0=ok
    uint32_t SNOfDevice;
    uint64_t SNOfSOM;
    uint32_t hwVersion;
    uint32_t bootloadVersion;
    uint32_t firmwareVersion;
    uint32_t fpgaVersion;
    uint32_t productDate;
    uint32_t calibrationDate;
    uint8_t   macAddr[12];
    uint32_t  ipAddr;
    uint32_t   SNOfPCB;
}resIdentification;
typedef struct DATA_OBJECT
{
    uint16_t id;
    uint16_t type;
    void *buffer;
}dataObject;
typedef struct DATA_MESSAGE_HEAD
{
    uint8_t  cmd;       //DATA_MESSAGE_D2H
    uint8_t invokeID;
    uint16_t reserved;
    uint32_t length;
    uint32_t  objCnts;
}dataMsgHead;
typedef struct REQ_READ_OBJ
{
    uint8_t  cmd;       //DATA_MESSAGE_D2H
    uint8_t invokeID;
    uint16_t reserved;
    uint32_t length;
    uint32_t  objCnts;
    uint16_t  objIds[32];   //iDS
}reqReadObj;
typedef struct REQ_WRITE_OBJ
{
    uint8_t  cmd;       //DATA_MESSAGE_D2H
    uint8_t invokeID;
    uint16_t reserved;
    uint32_t length;
    uint32_t  objCnts;
}reqWriteObj;
typedef struct RES_WRITE_OBJS   //0x93 Response
{
    uint8_t  cmd;
    uint8_t     invokeId;
    uint16_t    reserved;
    uint32_t    status;
    uint32_t    dataLength;
}resWriteObjs;
typedef struct RES_READ_OBJS    //reponse readobjs
{
    uint8_t  cmd;
    uint8_t     invokeId;
    uint16_t    reserved;
    uint32_t    status;
    uint32_t    dataLength;
    uint32_t    objNums;
    char        objBuffer[512];
}resReadObjs;

typedef struct OBJ_LIST
{
    struct OBJECT_HEAD head;
    struct OBJ_LIST *next;

}objList;

typedef struct REQ_WRITE_DATA_OBJECT_STRUCT
{
    uint8_t cmd;    //=REQ_WRITE_DATA_OBJECT;
    uint8_t invokeId;
    uint16_t resvered;
    uint32_t dataLength;
    uint32_t objCnts;
    objList *objs;
}reqWriteObjList;
typedef struct DATA_OBJ_DISTANCE
{
    uint16_t id;    //=OBJ_ID_DISTANCE;

}dataObjDistance;
typedef struct DATA_OBJECT_DATA_TO_SEND
{
    struct DATA_MESSAGE_HEAD head;
    uint16_t id;        //=OBJ_ID_DATA_TO_SEND;
    uint16_t   reversion;       //=R_VERSION;
    uint8_t    acousticPicturesEn;
    uint8_t videoPicturesEn;
    uint8_t localSoundEn;
    uint8_t micLevelsEn;
    uint8_t micRawDataEn;
    uint8_t spectraEn;
    uint8_t singleMicDataEn;
    uint8_t localSoundFilteredEn;
    uint8_t levelFilteredEn;
    uint8_t rawDataFilteredEn;
    uint8_t tachoEn;
    uint8_t hwTrigerEn;
}dataObjSend;
typedef struct MIC_POS
{
    uint8_t  id;
    float posx;
    float posy;
    float posz;
}micPos;


typedef struct RES_STATUS_STRUCT
{
    uint8_t     cmd;
    uint8_t     invokeID;
    uint16_t    resvered;
    uint32_t    status;
    uint32_t    dataLen;

}resStatus;

struct OBJ_ST_CAMERA_RESOLUTION
{
    uint16_t    objID;
    uint16_t    typeID;
    uint16_t    horResolution;
    uint16_t    vertResolution;
};
struct OBJ_ST_CAMERA_BRIGHTNESS
{
    uint16_t    objID;
    uint16_t    typeID;
    uint32_t    brightness;
};
struct OBJ_ST_ACOUSTIKFRAMERATE
{

};
typedef struct OBJ_ST_COMMONSTATUS
{
    struct DATA_MESSAGE_HEAD    head;
    uint16_t    objID;
    uint16_t    typeID;
    uint32_t    length;
    uint32_t    errorCode;
    uint32_t    deviceState;
    uint32_t    subState;
}objStCommonStatus;
typedef struct OBJ_ST_REPLY_MESSAGE
{
    struct DATA_MESSAGE_HEAD    head;
    uint16_t    objID;
    uint16_t    typeID;
    uint32_t    length;
}objStReplyMessage;
typedef struct OBJ_ST_MESSAGE_TYPE
{
    struct DATA_MESSAGE_HEAD    head;
    uint16_t    objID;
    uint16_t    typeID;
    uint32_t    length;
}objStMessageType;

typedef struct OBJ_ST_RAWDATA_STREAM
{
    struct DATA_MESSAGE_HEAD    head;
    uint16_t    objID;          //0xc002
    uint16_t    typeID;         //0xc001
    uint32_t    length;         //34+NxM Data Length
    uint16_t    groupID;        //group ID of data set
    uint16_t    channelsNums;   //N channels nums
    uint16_t    samplesNums;    //M samples nums
    uint16_t    dataSize;       //int32 float double,int8 int16
    uint16_t    mode;       //0=raw  1= clibrated
    uint16_t    reserved;       //Multiple of 4 byte filler
    float      lsb;            //scale factor,Least significant Bit
    uint64_t    ts;      //number of samples since program start
    float      dt;         //double dt=1/sample frequency
    float      factor;     //time factor
}objStRawData;

typedef struct OBJ_ST_ONE_DATA     //
{
    struct DATA_MESSAGE_HEAD head;
    uint16_t    objID;    //    //OBJ_ID_RAW_DATA
    uint16_t    typeID;       //=R_VERSION;
    uint32_t    length;       //leftLength
    uint64_t    ts;
    uint16_t    micIndex;          //
    float      dt;        // double dt=1/sample frequency
    float      factor;     //time factor

}objStOneData;
typedef struct OBJ_ST_ONE_FFT     // one fft frame
{
    struct DATA_MESSAGE_HEAD head;
    uint16_t    objID;       //OBJ_ID_SPECTRUMDATA
    uint16_t    typeID;       //=R_VERSION;
    uint32_t    length;       //Remaining length
    uint64_t    ts;
    uint16_t    micIndex;          //
    float      df;        // double dt=1/sample frequency
    uint8_t     filtered;       //
    uint32_t    freqMin;        //
    uint32_t    freqMax;        //
    uint32_t    fftSize;
}objStOneFFT;       //spectrumData

typedef struct OBJ_ST_RAW_FFT
{
    struct DATA_MESSAGE_HEAD head;
    uint16_t    objID;    //=OBJID_RAWDATA_STREAM;
    uint16_t    typeID;       //=R_VERSION;
    uint32_t    length;       //leftLength
    uint64_t    ts;
    uint16_t    channelsNums;

    uint8_t     filtered;       //
    float       df;          //
    uint32_t    freqMin;        //
    uint32_t    freqMax;        //
    uint32_t    fftSize;
}objStRawFFT;
typedef struct OBJ_ST_JPEG
{
    struct DATA_MESSAGE_HEAD head;
    uint16_t    objID;       //OBJ_ID_VIDEO_DATA
    uint16_t    typeID;       //=R_VERSION;
    uint32_t    length;       //leftLength
    uint64_t    ts;
    uint16_t    hPix;
    uint16_t    vPix;
}objStJpeg;
typedef struct OBJ_ST_ACOUSTIC
{
    struct DATA_MESSAGE_HEAD head;
    uint16_t    objID;       //OBJ_ID_ACOUSTIC_VIDEO_DATA
    uint16_t    typeID;       //=R_VERSION;
    uint32_t    length;       //Remaining length
    uint64_t    ts;
    uint32_t    freqMin;
    uint32_t    freqMax;       //
    uint32_t    distance;          //
	uint16_t	hPix;
	uint16_t	vPix;
}objStAcoustic;
typedef struct OBJ_ST_BEAMINFO
{
    struct DATA_MESSAGE_HEAD head;
    uint16_t    objID;       //OBJ_ID_ACOUSTIC_VIDEO_DATA
    uint16_t    typeID;       //=R_VERSION;
    uint32_t    length;       //Remaining length
    struct      BEAM_INFO info;

}objStBeaminfo;
//OBJ_ID_RAW_DATA
#pragma pack()

#define OBJ_HEAD_TITLE  8
#define OBJ_BODY_TITLE  12
struct  OBJ_HEAD_LENGTHS
{
    uint32_t    LEN_RAW_DATA;
    uint32_t    LEN_ONE_DATA;
    uint32_t    LEN_RAW_FFT;
    uint32_t    LEN_ONE_FFT;
    uint32_t    LEN_JPEG;
    uint32_t    LEN_ACOUS;
    uint32_t    LEN_COMMON_STAUS;
    uint32_t    LEN_BEAM_INFO;
	uint32_t    LEN_REPLY_MESSAGE;
    uint32_t    BODY_RAW_DATA;
    uint32_t    BODY_ONE_DATA;
    uint32_t    BODY_RAW_FFT;
    uint32_t    BODY_ONE_FFT;
    uint32_t    BODY_JPEG;
    uint32_t    BODY_ACOUS;
};




#endif // DATA_STRUCT_H
